﻿/// <reference path="../Libriries/jquery-2.0.3.js" />
/// <reference path="../Libriries/q.js" />
/// <reference path="class.js" />
/// <reference path="topic-tree.js" />

var languageController = (function () {
    var DEFAULT_LANGUAGE = "BG";
    var selectedLanguage = "BG";

    var errorFunction = function (error) {
        $(document.body).html("<span style='color: white; font-size: 25px;'>Loading this site was encountered an error." +
            " Please contact administrator if possible - abrashev92@gmail.com</span>");
        console.log(error);
    }

    var load = function (callFunction, delay) {
        selectedLanguage = localStorage.getItem("minc-language");

        if (!selectedLanguage) {
            selectedLanguage = DEFAULT_LANGUAGE;
        }

        var selectMenu = $("#language_select");

        selectMenu.val(selectedLanguage.toLowerCase());

        eval(callFunction + "('" + selectedLanguage + "', '" + delay + "')");
    }

    var loadIndex = function(language) {
        $.ajax({
            type: "get",
            url: "../Resources/XmlRes/" + language + "/index-content.xml",
            dataType: 'xml',
            success: function (xml) {
                var finder = $(xml).find("h1-about-us").text();
                $("#h1-about-us").text(finder);

                finder = $(xml).find("li-about-us").text();
                $("#li-about-us").text(finder);
                
                finder = $(xml).find("li-about-this-site").text();
                $("#li-about-this-site").text(finder);

                finder = $(xml).find("li-some-ideas").text();
                $("#li-some-ideas").text(finder);

                finder = $(xml).find("h1-programming").text();
                $("#h1-programming").text(finder);

                finder = $(xml).find("li-projects").text();
                $("#li-projects").text(finder);

                finder = $(xml).find("li-vs-tips").text();
                $("#li-vs-tips").text(finder);

                finder = $(xml).find("li-learning-materials").text();
                $("#li-learning-materials").text(finder);

                //finder = $(xml).find("li-telerik-homeworks").text();
                //$("#li-telerik-homeworks").text(finder);

                finder = $(xml).find("li-it-articles").text();
                $("#li-it-articles").text(finder);

                finder = $(xml).find("h1-games").text();
                $("#h1-games").text(finder);

                finder = $(xml).find("li-learning-games").text();
                $("#li-learning-games").text(finder);

                finder = $(xml).find("li-html5-games").text();
                $("#li-html5-games").text(finder);

                finder = $(xml).find("li-bounce-game").text();
                $("#li-bounce-game").text(finder);
            },
            error: function (error) {
                errorFunction(error);
            }
        });
    }

    var loadAboutUs = function (language, delay) {
        $.ajax({
            type: "get",
            url: "../Resources/XmlRes/" + language + "/about-us-content.xml",
            dataType: 'xml',
            success: function (xml) {
                var topicsHolderHeader = $(xml).find("topicsHolderHeader").text()
                $("#topicsHolderHeader").text(topicsHolderHeader);

                TopicTree.changeArticlesLanguage(language);
                var topicTree = $(xml).find("topicTree");

                var topicsHolder = topicTree.attr("topicsHolder");
                var articleHeader = topicTree.attr("articleHeader");
                var articleHolder = topicTree.attr("articleHolder");
                
                var visualTopicTree = new TopicTree.TreeClass(topicsHolder, articleHeader, articleHolder);

                $(xml).find("topicTree topic").each(function() {
                    var name = $(this).attr("name");
                    var article = $(this).attr("article");

                    var topic = visualTopicTree.addTopic(name, article);

                    $(this).find("subtopic").each(function () {
                        var subtopic = $(this).text();
                        var id = $(this).attr("id");

                        topic.addSubtopic(subtopic, id);
                    });
                });

                if (delay != "false" && delay != false) {
                    setTimeout(function () {
                        visualTopicTree.render();
                    }, 1500);
                }
                else {
                    visualTopicTree.render();
                }
            },
            error: function (error) {
                errorFunction(error);
            }
        });
    }

    var loadProgramming = function (language) {
        $.ajax({
            type: "get",
            url: "../Resources/XmlRes/" + language + "/programming-content.xml",
            dataType: 'xml',
            success: function (xml) {
                var finder = $(xml).find("li-learning-materials").text();
                $("#li-learning-materials").text(finder);

                finder = $(xml).find("li-visual-studio-projects").text();
                $("#li-visual-studio-projects").text(finder);

                finder = $(xml).find("li-it-articles").text();
                $("#li-it-articles").text(finder);
            },
            error: function (error) {
                errorFunction(error);
            }
        });

        var lm = $("#learning-materials");
        if (lm) {
            loadLearningMaterials(language);
            LecturesMaker.changeLecturesLanguage(language);
            LectureController.changeLectureLanguage(language);
            CoursesMaker.makeCourses("#courses-content", "#lectures-content");
        }
    }

    var loadLearningMaterials = function (language) {
        $.ajax({
            type: "get",
            url: "../Resources/XmlRes/" + language + "/Programming/learning-materials-content.xml",
            dataType: 'xml',
            success: function (xml) {
                var finder = $(xml).find("courses-header").text();
                $("#courses-header").text(finder);

                finder = $(xml).find("lectures-header").text();
                $("#lectures-header").text(finder);
            },
            error: function (error) {
                errorFunction(error);
            }
        });
    }

    return {
        load: load
    }

}());

function languageChanged(language, callFunction, delay) {
    //var sender = event.target || event.srcElement;
    //var language = sender.options[sender.selectedIndex].value;
    
    localStorage.setItem("minc-language", language);
    languageController.load(callFunction, delay);
}